package test_front.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import test_common.entity.User;
import test_common.utils.ResponseResult;
import test_front.sevice.UserService;
import test_front.utils.ContentIsNull;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/register")
    public ResponseResult register(User user){

        ResponseResult result=new ResponseResult();
        if (ContentIsNull.judgeString(user.getLoginName())||ContentIsNull.judgeString(user.getPassword())
        ||ContentIsNull.judgeString(user.getEmail())){
            result.setMessage("有必填内容未填写");
            result.setStatus(-1);
            return result;

        }
        userService.register(user);
        result.setStatus(1);
        result.setMessage("注册成功！");
        return result;
    }

    @ResponseBody
    @RequestMapping("/selectLoginName")
    public ResponseResult selectLoginName(String loginName){
        ResponseResult result=new ResponseResult();
        User user=userService.selectLoginName(loginName);
        if (user==null){
            result.setStatus(1);
            result.setMessage("登录名有效");
            return result;
        }
        result.setStatus(-1);
        result.setMessage("登录名已经使用，请换一个！");
        return result;
    }

    @ResponseBody
    @RequestMapping("/login")
    public ResponseResult login(User user,HttpServletRequest request ){
        ResponseResult result=new ResponseResult();
        User user1=userService.selectByNameAndPassword(user);
        if (user1==null){
            result.setStatus(-1);
            result.setMessage("用户名或密码错误！");
            return result;
        }
        result.setStatus(1);
        result.setMessage("身份和对正确！");
        result.setObject(user1);
        request.getSession().setAttribute("user",user1);
        user1.setSignDate(new Date());
        userService.insertLoginDate(user1);
        return result;

    }
    @RequestMapping("/personInfo")
    @ResponseBody
    public ResponseResult personInfo(HttpServletRequest request){
        ResponseResult result= new ResponseResult();
        String userId= request.getParameter("userId");


        return  result;

    }
}
